**********************************************************************************	
*** TABLE A5: Local projection-DiD results
**********************************************************************************

use "$DATALOC/AgencyLevel.dta", clear
*++++++++++++
* LP-DiD variable creation.
*++++++++++++
  sort agencyid FY
  * Difference of D treatment variable from t-1 to t 
  qui g DDit = D1.agen_hear_all
  label var DDit "Agency hearing, t-1 to t"
  * Difference of Y response variable from t-1 to t+k.
  forvalues k = 0/3 {
    qui by agencyid (FY): g y_`k'_D1_y = F`k'.iprate - L1.iprate
    if (`k' == 0) {
      label var y_`k'_D1_y "Improper Payment Rate, t-1 to t"
    }
    else {
      label var y_`k'_D1_y "Improper Payment Rate, t-1 to t-`k'"
    }
  }
  summ y_*D1* DDit

  * Set up lag differences of Y to t and Z to t.
  forvalues l = 1/2 {
    * Y.
    qui by agencyid (FY): g L`l'_y_0_D1_y = L`l'.y_0_D1_y
    local lminus1 = `l' + 1
    label var L`l'_y_0_D1_y "Improper Payment Rate, t-`lminus1' to t-`l'"
    * Z: last_agen_hear
    foreach var of varlist last_agen_hear {
      qui g DZ`l'_`var' = D`l'.`var'
    }
    local lminus1 = `l' + 1
    label var DZ`l'_last_agen_hear "Years Since Last Agency Hearing, t-`lminus1' to t-`l'"
  }

  * Dube et al estimator limits to cases where DDit != 0 (treated) OR 
  * DDit \approx 0 for t = (k,k-1,k-2,...,k-H) (clean controls).
  qui g clean_control = DDit == 0
  tab clean_control
  qui g treated = 1 - clean_control
  * Calculate for different values of H.
  sort agencyid FY
  qui by agencyid (FY): g clean_control_H1 = clean_control == 1 & L1.clean_control == 1
  qui by agencyid (FY): g clean_control_H2 = clean_control == 1 & L1.clean_control == 1 & L2.clean_control == 1
  qui by agencyid (FY): g clean_control_H3 = clean_control == 1 & L1.clean_control == 1 & L2.clean_control == 1 & L3.clean_control == 1
  summ clean_control*

*++++++++++++
* Lp-DiD regressions.
*++++++++++++

* Clear existing estimates.
eststo clear

* Treatment effects with different definitions of clean controls.
forvalues H = 1/3 {
	eststo: reghdfe y_0_D1_y DDit if (treated == 1 | clean_control_H`H' == 1), absorb(FY) $ROBUST_SE
	quietly estadd local fixedyear "Yes", replace
	estadd ysumm, replace
}

* Treatment effects with different definitions of clean controls AND lagged D_Y.
forvalues H = 1/3 {
	eststo: reghdfe y_0_D1_y DDit L*_y_0_D1_y if (treated == 1 | clean_control_H`H' == 1), absorb(FY) $ROBUST_SE
	quietly estadd local fixedyear "Yes", replace
	estadd ysumm, replace
}

* Table fragment.
esttab using "$TABLELOC/TableA05_agency.tex", ///
	b(a2) fragment ///
	booktabs label se star(** 0.05 *** 0.01)s(fixedyear N, label("Fiscal Year FE" "Observations")) ///
	nogaps nolines nomtitle nonumbers ///
	prefoot("\midrule") postfoot("\midrule") replace

*++++++++++++
* PROGRAM-LEVEL.
*++++++++++++

use "$DATALOC/ProgramLevel.dta", clear
*++++++++++++
* LP-DiD variable creation.
*++++++++++++
  sort programid FY
  * Difference of D treatment variable from t-1 to t 
  qui g DDit = D1.agen_hear_all
  label var DDit "Agency hearing, t-1 to t"
  * Difference of Y response variable from t-1 to t+k.
  forvalues k = 0/3 {
    qui by programid (FY): g y_`k'_D1_y = F`k'.iprate - L1.iprate
    if (`k' == 0) {
      label var y_`k'_D1_y "Improper Payment Rate, t-1 to t"
    }
    else {
      label var y_`k'_D1_y "Improper Payment Rate, t-1 to t-`k'"
    }
  }
  summ y_*D1* DDit

  * Set up lag differences of Y to t and Z to t.
  forvalues l = 1/2 {
    * Y.
    qui by programid (FY): g L`l'_y_0_D1_y = L`l'.y_0_D1_y
    local lminus1 = `l' + 1
    label var L`l'_y_0_D1_y "Improper Payment Rate, t-`lminus1' to t-`l'"
    * Z: last_agen_hear
    foreach var of varlist last_agen_hear {
      qui g DZ`l'_`var' = D`l'.`var'
    }
    local lminus1 = `l' + 1
    label var DZ`l'_last_agen_hear "Years Since Last Agency Hearing, t-`lminus1' to t-`l'"
  }

  * Dube et al estimator limits to cases where DDit != 0 (treated) OR 
  * DDit \approx 0 for t = (k,k-1,k-2,...,k-H) (clean controls).
  qui g clean_control = DDit == 0
  tab clean_control
  qui g treated = 1 - clean_control
  * Calculate for different values of H.
  sort programid FY
  qui by programid (FY): g clean_control_H1 = clean_control == 1 & L1.clean_control == 1
  qui by programid (FY): g clean_control_H2 = clean_control == 1 & L1.clean_control == 1 & L2.clean_control == 1
  qui by programid (FY): g clean_control_H3 = clean_control == 1 & L1.clean_control == 1 & L2.clean_control == 1 & L3.clean_control == 1
  summ clean_control*

*++++++++++++
* Lp-DiD regressions.
*++++++++++++

* Clear existing estimates.
eststo clear

* Treatment effects with different definitions of clean controls.
forvalues H = 1/3 {
	eststo: reghdfe y_0_D1_y DDit if (treated == 1 | clean_control_H`H' == 1), absorb(FY) $ROBUST_SE
	quietly estadd local fixedyear "Yes", replace
	estadd ysumm, replace
}

* Treatment effects with different definitions of clean controls AND lagged D_Y.
forvalues H = 1/3 {
	eststo: reghdfe y_0_D1_y DDit L*_y_0_D1_y if (treated == 1 | clean_control_H`H' == 1), absorb(FY) $ROBUST_SE
	quietly estadd local fixedyear "Yes", replace
	estadd ysumm, replace
}

* Table fragment.
esttab using "$TABLELOC/TableA05_program.tex", ///
	b(a2) fragment ///
	booktabs label se star(** 0.05 *** 0.01)s(fixedyear N, label("Fiscal Year FE" "Observations")) ///
	nogaps nolines nomtitle nonumbers ///
	prefoot("\midrule") postfoot("\midrule") replace
	
	
	
